package com.appheader.rongmaphone.adapter;

import android.content.Context;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.appheader.rongmaphone.R;
import com.appheader.rongmaphone.entity.Project;
import com.appheader.rongmaphone.utils.ScreenUtils;
import com.appheader.rongmaphone.utils.network.FileUtils;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;
import java.util.List;
/**
 * Created by zhep on 2016/10/23.
 */
public class OffLineAdapter extends BaseAdapter {
    private List<Project> list;
    private Context context;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    private String mSdRootPath = Environment.getExternalStorageDirectory() + File.separator + FileUtils.IMAGE_PATH + File.separator;
    public OffLineAdapter(List<Project> list, Context context,DisplayImageOptions options,ImageLoader imageLoader) {
        this.list = list;
        this.context = context;
        this.options = options;
        this.imageLoader = imageLoader;
    }
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        OffLineViewHolder holder = null;
        if (convertView == null) {
            holder = new OffLineViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.main_item, parent, false);
            holder.img = (RoundedImageView) convertView.findViewById(R.id.cloud_imageView);
            holder.textView = (TextView) convertView.findViewById(R.id.cloud_textView);
            convertView.setTag(holder);
        } else {
            holder = (OffLineViewHolder) convertView.getTag();
        }
        holder.textView.setText(list.get(position).getName());
        String imgUrl = list.get(position).getPicId();
//        String imgPath = mSdRootPath + FileUtils.getImageName(imgUrl);
//        Log.e("ADAPTERTAG", "getView: " + imgPath+"-------------"+imgUrl);
        ViewGroup.LayoutParams layoutParams = holder.img.getLayoutParams();
        //获得的
        layoutParams.height = ScreenUtils.getScreenHeight(context)/2;//一屏幕显示两行
        layoutParams.width = (ScreenUtils.getScreenWidth(context)-20)/2;//一屏幕显示两列
        holder.img.setLayoutParams(layoutParams);

        //方法一、从本地获取图片数据
//        File file = new File(mSdRootPath + FileUtils.getImageName(imgUrl));
//        if (file.i)
//        Log.e("OffLineADag", "getView: -----" + file.exists());
//        holder.img.setImageBitmap(FileUtils.getBitmap(imgPath));
        //方法二、
//        try {
//            FileInputStream fis = new FileInputStream(mSdRootPath+FileUtils.getImageName(imgPath));
//            Bitmap bitmap = BitmapFactory.decodeStream(fis);
//            holder.img.setImageBitmap(bitmap);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
//            }
//        }
        // http://i.armyhorse.com/5774975de182da0fd82f364c/files/image/FIL20BA37D5-725A-462A-9117-6FF536EDE344.png
        //方法三、imageloader
        imageLoader.displayImage(imgUrl,holder.img,options);
        return convertView;
    }
    class OffLineViewHolder {
        private RoundedImageView img;
        private TextView textView;
    }
}
